package com.ph.irp.stock.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.ph.irp.model.PStockStockinvoicedetail;
import com.ph.irp.stock.dao.StockInvoiceDetailDAO;
import com.ph.util.DBUtil;

public class StockInvoiceDetailDAOImpl implements StockInvoiceDetailDAO {

	private ComboPooledDataSource dataSource;

	public ComboPooledDataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(ComboPooledDataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	@Override
	public void addStockInvoiceDetail(
			PStockStockinvoicedetail stockInvoiceDetailModel) throws Throwable {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		
		try {
			StringBuilder sBuilder = new StringBuilder();
			sBuilder.append("insert into p_stock_stockinvoicedetail");
			sBuilder
					.append("(StockInvoiceID,MaterialID,BasicUnitAmount,MeasureUnitId,Amount,UnitPrice,TotalUnitPrice,DutyFrank,Discount,RealTotalUnitPrice,Money,MoneyBW,DiscountPrice," +
							"DiscountPriceBW,DutyPrice,DutyPriceBW,TotalMoney,TotalMoneyBW,InCostBW,CostBW,TrafficDutyBW,SourceBillCode,SourceOrderCode,OrderTotalUnitPrice,InStorageAmount,SpuerCode,Remark)");
			sBuilder.append(" values (");
			sBuilder.append(stockInvoiceDetailModel.getStockInvoiceId());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getMaterialId());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getBasicUnitAmount());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getMeasureUnitId());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getAmount());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getUnitPrice());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getTotalUnitPrice());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getDutyFrank());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getDiscount());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getRealTotalUnitPrice());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getMoney());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getMoneyBw());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getDiscountPrice());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getDiscountPriceBw());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getDutyPrice());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getDutyPriceBw());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getTotalMoney());
			sBuilder.append(",");
			sBuilder.append(stockInvoiceDetailModel.getTotalMoneyBw());
			sBuilder.append(",");
			if(stockInvoiceDetailModel.getInCostBw()!=0)
				sBuilder.append(stockInvoiceDetailModel.getInCostBw());
			else
				sBuilder.append("null");
			sBuilder.append(",");
			
			if(stockInvoiceDetailModel.getCostBw()!=0)
				sBuilder.append(stockInvoiceDetailModel.getCostBw());
			else
				sBuilder.append("null");
			sBuilder.append(",");
			
			if(stockInvoiceDetailModel.getTrafficDutyBw()!=0)
				sBuilder.append(stockInvoiceDetailModel.getTrafficDutyBw());
			else
				sBuilder.append("null");
			sBuilder.append(",");
			if(stockInvoiceDetailModel.getSourceBillCode()!=null)
				sBuilder.append(stockInvoiceDetailModel.getSourceBillCode());
			else
				sBuilder.append("null");
			sBuilder.append(",");
			
			if(stockInvoiceDetailModel.getSourceOrderCode()!=null)
				sBuilder.append(stockInvoiceDetailModel.getSourceOrderCode());
			else
				sBuilder.append("null");
			sBuilder.append(",");
			
			if(stockInvoiceDetailModel.getOrderTotalUnitPrice()!=0)
				sBuilder.append(stockInvoiceDetailModel.getOrderTotalUnitPrice());
			else
				sBuilder.append("null");
			sBuilder.append(",");
			
			if(stockInvoiceDetailModel.getInStorageAmount()!=0)
				sBuilder.append(stockInvoiceDetailModel.getInStorageAmount());
			else
				sBuilder.append("null");
			sBuilder.append(",");
			
			if(stockInvoiceDetailModel.getSpuerCode()!=null)
				sBuilder.append(stockInvoiceDetailModel.getSpuerCode());
			else
				sBuilder.append("null");
			sBuilder.append(",");
			
			if(stockInvoiceDetailModel.getRemark()!=null && stockInvoiceDetailModel.getRemark()!="")
				sBuilder.append(stockInvoiceDetailModel.getRemark());
			else
				sBuilder.append("null");
			sBuilder.append(")");
			System.out.println(sBuilder.toString());
			con = dataSource.getConnection();

			pStmt = con.prepareStatement(sBuilder.toString());

			pStmt.executeUpdate();
	} catch (Exception e) {
		throw new Exception(e.getMessage());
	} finally {
		try {
			DBUtil.setNullObject(con, pStmt, rs);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}
	}

	@Override
	public void deleteStockInvoiceDetail(int stockInvoiceDetailID)
			throws Throwable {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<PStockStockinvoicedetail> getAllStockInvoiceDetail()
			throws Throwable {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PStockStockinvoicedetail getStockInvoiceDetailByID()
			throws Throwable {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateStockInvoiceDetail(
			PStockStockinvoicedetail stockInvoiceDetailModel) throws Throwable {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<PStockStockinvoicedetail> getStockInvoiceDetailByModel(PStockStockinvoicedetail stockInvoiceDetailModel)
			throws Throwable {
		// TODO Auto-generated method stub
		return null;
	}


}
